package details

import (
	"github.com/goAdminMicroService/internal/pkg/app"
	"github.com/goAdminMicroService/internal/pkg/transports/grpc"
	"github.com/goAdminMicroService/internal/pkg/transports/http"
	"github.com/google/wire"
	"github.com/pkg/errors"
	"github.com/spf13/viper"
	"go.uber.org/zap"
)

type Options struct {
	Name string
}

// 初始化选项
func NewOptions(v *viper.Viper, logger *zap.Logger) (*Options, error) {
	var (
		err error
		o   = new(Options)
	)
	if err = v.UnmarshalKey("app", o); err != nil {
		return nil, errors.Wrap(err, "Unmarshal app option error")
	}
	logger.Info("load application option success")
	return o, nil
}

// 创建 detail app
func NewApp(o *Options, logger *zap.Logger, hs *http.Server, gs *grpc.Server) (*app.Application, error) {
	a, err := app.New(o.Name, logger, app.GrpcServerOption(gs), app.HttpServerOption(hs))
	if err != nil {
		return nil, errors.Wrap(err, "newApp error")
	}
	return a, nil
}

var ProviderSet = wire.NewSet(NewApp, NewOptions)
